package com.qf.lanzhou.blog.api.controller.comment;

import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.CommentService;
import com.qf.lanzhou.blog.api.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 是柠檬甜
 * @ClassName CommentServlet
 * @description 评论功能
 * @date 2020/12/17 17:37
 * @modifier 是柠檬甜
 * @date 2020-12-17 17:37
 * @Version V1.0
 */
@WebServlet("/comment.do")
public class CommentServlet extends BaseServletPost {
    private CommentService commentService=new CommentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commentContent = req.getParameter("commentContent");
        String articleId = req.getParameter("articleId");
        User user = (User) req.getSession().getAttribute("user");
        long articleIdLong = Long.parseLong(articleId);
        try{
            String message = commentService.addComment(commentContent, user.getUserName(), articleIdLong);
            successResultWrite(message,resp);
        }catch (RuntimeException e){
            String errorMsg = e.getMessage();
            failedResultWrite(errorMsg,resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
